from selenium import webdriver
import time
from bs4 import BeautifulSoup
import xlrd
import xlwt
import openpyxl
import excel

def main():
    chrome_driver = 'C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe'  #chromedriver的文件位置
    driver = webdriver.Chrome(executable_path = chrome_driver)
  	#driver.maximize_window()
    driver.get("http://quotes.money.163.com/old/#query=EQA&DataType=HS_RANK&sort=PERCENT&order=desc&count=24&page=0")#进入目标网页
    windows = driver.window_handles
    '''
    获取所需数据
    testdata为股票编号
    testname为股票名称
    testsave为保存名称
    '''
    data = xlrd.open_workbook('C:\\Users\\chenzhen\\Desktop\\test.xlsx')
    sheet = data.sheet_by_name('Sheet1')#通过excel表格名称(rank)获取工作表
    print(sheet.row(1)[1])#编号
    print(sheet.row(1)[2])#名称


    for i in range(2):
        testdata = (sheet.row(i+1)[1])
        testname = (sheet.row(i+1)[2])
        testdata = str(testdata)
        testdata = testdata.strip("text:").strip("'")
        testname = str(testname)
        testname = testname.strip("text:").strip("'")
        testsave = testname+testdata
        driver.maximize_window()
        print(testname)
        print(testdata)
        time.sleep(10)
        driver.find_element_by_xpath("//*[@id='hsRank']/div[2]/div[2]/form/input[1]").clear()
        driver.find_element_by_xpath("//*[@id='hsRank']/div[2]/div[2]/form/input[1]").send_keys(testdata)#向目标搜索栏送入数据
        time.sleep(5)
        print("input success")
        driver.find_element_by_name('searchbtn').click()
        print("search success")
        time.sleep(10)
        driver.find_element_by_xpath("//*[@id='hsRank']/div[2]/div[3]/div/div[1]/div[2]/div[2]/div/div/table[2]/tbody/tr/td[2]/a").click()
        time.sleep(20)
        windows = driver.window_handles
        driver.switch_to.window(windows[-1])
        print("login success")
        driver.get_screenshot_as_file("C:\\Users\\chenzhen\\Desktop\\testpic\\%s.png"%(testsave))
        time.sleep(5)
        # b.get(url)
        # currentPageUrl = self.driver.current_url
        #driver.quit()
        driver.switch_to.window(windows[0])
    driver.quit()

if __name__ == '__main__':
    main()


    
# coding=utf-8
# import time
# from selenium import webdriver
 
 
# driver = webdriver.Chrome()
# driver.maximize_window()
# driver.implicitly_wait(6)
 
# driver.get("http://www.baidu.com/")
# time.sleep(10)
# driver.find_element_by_link_text("新闻").click()
# time.sleep(10)
# print (driver.current_url)  # current_url 方法可以得到当前页面的URL
# driver.quit()

